﻿/*
 *站内信
 *需要引用jquery
*/
!window.jQuery && alert("请添加jQuery引用");
define(function () {
    var options = {
        URL: ""
        , Delay: 30000
    };

    function Message(opt) {
        this.URL = opt.URL;
        this.Delay = opt.Delay || options.Delay;
        this.onReceived = null;
        this.IsPageLoad = true; //是否是第一次加载
    }

    Message.prototype = {
        constructor: Message,
        //启动消息推送
        run: function () {
            this.doReceive();
            this.IsPageLoad = false;
        },
        doReceive: function () {
            var _self = this;
            $.get(_self.URL, { IsPageLoad: _self.IsPageLoad, Timestamp: new Date - 0 }, function (rst) {
                if (rst && rst.length > 0) {
                    _self.onReceived(rst);
                }
            }, "json").complete(function () {
                setTimeout(function () {
                    _self.doReceive();
                }, _self.Delay);
            });
        },
        //接收消息
        receive: function (cb) {
            this.onReceived = cb;
        },
        //发送消息
        send: function (SendData, _callback) {
            var _self = this;
            SendData.Timestamp = new Date - 0;
            $.post(_self.URL, SendData, function (data) {
                _callback(data);
            }, "json");
        }
    }
    return Message;
});